package cc.mrbird.system.log.controller;


import cc.mrbird.common.domin.Message;
import cc.mrbird.common.domin.QueryRequest;
import cc.mrbird.common.log.Log;
import cc.mrbird.common.poi.ExcelUtil;
import cc.mrbird.common.service.RedisService;
import cc.mrbird.common.utils.DateUtils;
import cc.mrbird.system.log.model.Loginfo;
import cc.mrbird.system.log.service.LoginfoService;
import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yux
 * @since 2019-07-16
 */
@Controller
@RequestMapping("/loginfo")
@Api(tags = "日志信息类")
public class LoginfoController {
    @Resource
    public LoginfoService loginfoService;

    @Resource
    public RedisService redisService;

    @Log("日志导出")
    @GetMapping("/list")
    public void list(HttpServletResponse respons) {
        Wrapper<Loginfo> queryWrapper = Wrappers.query();
        Page<Loginfo> page = new Page<>(1, 5);

        try {
            List<Loginfo> list = loginfoService.page(page, queryWrapper).getRecords();
            ExcelUtil<Loginfo> util = new ExcelUtil<>(Loginfo.class);
            util.exportExcel(list, "经开", respons, 0, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log("日志查询")
    @PostMapping("/list2")
    @ResponseBody
    public Message<List<Loginfo>> list2(QueryRequest queryRequest) {
        QueryWrapper<Loginfo> queryWrapper = Wrappers.query();
        queryWrapper.in("LogId", "1", "2", "3", "4", "5", "6");
        try {
            Page<Loginfo> page = loginfoService.page(new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize()), queryWrapper);
            return Message.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @Log("日志查询")
    @PostMapping("/list3")
    @ResponseBody
    @ApiOperation("日志信息查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logid", value = "日志ID", required = true, dataTypeClass = Integer.class)
    })
    public Message<List<Loginfo>> list3(String logid) {
        try {
            QueryWrapper<Loginfo> queryWrapper = Wrappers.query();
            queryWrapper.in("LogId", logid);
            return Message.ok(loginfoService.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    //@Log("日志查询分页1（mybitisplus自带）")
    @GetMapping("/list4")
    @ResponseBody
    @ApiOperation("日志查询分页1（mybitisplus自带）")
    public Message<List<Loginfo>> list4(QueryRequest queryRequest) {
        QueryWrapper<Loginfo> queryWrapper = Wrappers.query();
        queryWrapper.in("logid", 1, 2, 3, 4, 5, 6);
        try {
            return Message.ok(loginfoService.page(new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize()), queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

/*    //@Log("日志查询分页2（PageHelper）")
    @GetMapping("/list5")
    @ResponseBody
    @ApiOperation("日志查询分页2（PageHelper）")
    public Message<List<Loginfo>> list5(QueryRequest queryRequest) {
        QueryWrapper<Loginfo> queryWrapper = Wrappers.query();
        queryWrapper.in("logid", 1, 2, 3, 4, 5, 6);
        try {
            return Message.ok(PageUtil.selectByPageNumSize(queryRequest, () -> loginfoService.list(queryWrapper)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }*/

    //@Log("日志查询分页3（mybitisplus自带）")
    @GetMapping("/list6")
    @ResponseBody
    @ApiOperation("日志查询分页3（mybitisplus自带）")
    public Message<List<Loginfo>> list6(QueryRequest queryRequest) {
        QueryWrapper<Loginfo> queryWrapper = Wrappers.query();
        queryWrapper.in("logid", 1, 2, 3, 4, 5, 6);
        try {
            return Message.ok(loginfoService.selectMyPage(new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize()), queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @Log("日志删除")
    @PostMapping("/delete")
    @ResponseBody
    public Message<Boolean> delete(int id) {
        try {
            //double a=3/0;
            loginfoService.removeById(id);
            return Message.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @Log("日志添加")
    @PostMapping("/add")
    @ResponseBody
    public Message<Boolean> add() {
        try {
            Loginfo loginfo = new Loginfo();
            loginfo.setLogId(1);
            loginfoService.save(loginfo);
            return Message.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @PostMapping("/redis")
    @ResponseBody
    public Message<Boolean> redis() {
        redisService.set("name", "小明");
        System.out.println(redisService.get("name"));
        return null;
    }

    @Resource
    private Kaptcha kaptcha;

    @GetMapping("/render")
    @ApiOperation("验证码生成")
    public void render() {
        kaptcha.render();
    }

    @PostMapping("/valid")
    @ApiOperation("验证码验证")
    public void validDefaultTime(@RequestParam String code) {
        //default timeout 900 seconds
        System.out.println(kaptcha.validate(code));
    }

    @PostMapping("/validTime")
    @ApiOperation("验证码验证（超时判断）")
    public void validCustomTime(@RequestParam String code) {
        System.out.println(kaptcha.validate(code, 60));
    }

    @Log("事务测试")
    @PostMapping("/trans")
    @ApiOperation("事务测试")
    @ResponseBody
    public Message<Boolean> trans() {
        try {
            loginfoService.trans();
            return Message.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @Log("乐观锁实现")
    @PostMapping("/update")
    @ApiOperation("乐观锁实现")
    @ResponseBody
    public Message<Boolean> update() {
        try {
            Loginfo loginfo = loginfoService.getById(1);
            loginfo.setLogTitle(DateUtils.getTime());
            return Message.ok(loginfoService.updateById(loginfo));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error();
    }

    @GetMapping("/transactionalTest")
    @ResponseBody
    public Message transactionalTest() {
        loginfoService.transactionalTest();
        return Message.ok();
    }
}

